package com.bls.lib.permission;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;

import java.util.List;


public class PermissionActivity extends Activity {

    private String[] REQUEST_PERMISSIONS;
    private static final int REQUEST_PERMISSION_CODE_TAKE_PIC = 9; //权限的请求码
    private static final int REQUEST_PERMISSION_SETTING = 8; //去设置界面的请求码
    private PermissionActivity permissionActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        permissionActivity = this;
        Intent intent = getIntent();
        if (intent != null) {
            REQUEST_PERMISSIONS = intent.getStringArrayExtra("permission");
        }

        if (REQUEST_PERMISSIONS != null) {
            checkPermiss();
        } else {
            finish();
            if (Permission.mPermissionResult != null) {
                Permission.mPermissionResult.fail();
            }
        }
    }

    /**
     * 请求权限
     */
    private void requestPermission(final String[] permissions) {
        //请求权限  将已经开启的权限 在点拒绝 会杀死当前进程和在设置界面关闭一样
        PermissionUtil.requestContactsPermissions(PermissionActivity.this, permissions, REQUEST_PERMISSION_CODE_TAKE_PIC);
    }


    private void startToSetting() {
        DeleteDialog deleteDialog = new DeleteDialog(PermissionActivity.this, new DeleteDialog.onConfirmListener() {
            @Override
            public void confirm() {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
            }

            @Override
            public void cancel() {
                permissionActivity.finish();
            }
        });
        deleteDialog.setTvContent("去设置界面开启权限?");
        deleteDialog.show();
    }


    /**
     * 检测权限的回调
     */
    public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == REQUEST_PERMISSION_CODE_TAKE_PIC) {
            if (PermissionUtil.verifyPermissions(grantResults)) {
                finish();
                if (Permission.mPermissionResult != null) {
                    Permission.mPermissionResult.success();
                }

            } else {
                //没有权限
                if (!PermissionUtil.shouldShowPermissions(this, permissions)) {
                    startToSetting();
                } else {
                    if (Permission.mPermissionResult != null) {
                        Permission.mPermissionResult.fail();
                    }
                    permissionActivity.finish();
                }
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            finish();
            if (Permission.mPermissionResult != null) {
                Permission.mPermissionResult.fail();
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        permissionActivity.finish();
    }


    /**
     * 请求权限
     */
    private void checkPermiss() {
        PermissionUtil.checkPermission(this, REQUEST_PERMISSIONS, new PermissionUtil.PermissionInterface() {
            @Override
            public void success() {
                //请求成功
                finish();
                if (Permission.mPermissionResult != null) {
                    Permission.mPermissionResult.success();
                }
            }

            @Override
            public void fail(final List<String> permissions) {
                for (String s : permissions) {
                    System.out.println(s);
                }
                requestPermission(permissions.toArray(new String[permissions.size()]));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        REQUEST_PERMISSIONS = null;
        Permission.mPermissionResult = null;
    }
}
